<?php
//用户管理
namespace app\adminbackstage\controller;
use app\BaseController;
use app\adminbackstage\model\User;

//use think\facade\Session;
use think\facade\Request;

/**
 * Description of UserManagementController
 *
 * @author asus80
 */
class UserManagementController  extends BaseController {
   
    public function userList(){
        $page = Request::param('page');
        $num = Request::param('num');
        $type = Request::param('type');//类型 1后台管理员 2后台普通管理员 21 代理 22商家 23 达人
        

        $enable = Request::param('enable');
        $start = Request::param('starttime');
        $end = Request::param('endtime');
        $phone = Request::param('phone');
        $username = Request::param('username');
        //$store_name = Request::param('s_name');

        $a_uid = Request::param('a_uid');//管理员ID
        if(!empty($a_uid)){
            $user = new User();
            $where_u[] = ["u.uid",'=',$a_uid];
            $find = $user->find($where_u);
            if(empty($find)){
                return json(['code' => -200, 'msg' => lang('无菜单权限'), 'data' => '', 'count' => '']);
            }
            else{
                $find = $find->toArray();
                if($find['enable']!=1){
                    return json(['code' => -200, 'msg' => lang('无菜单权限'), 'data' => '', 'count' => '']);
                }
            }
        }

        $where = [];
        if(!empty($start)){
            $time_start = strtotime($start);
            $where[] = ['u.addtime','>=',$time_start];
        }
        
        if(!empty($end)){
            $time_end = strtotime($end)+(24*3600-1);
            $where[] = ['u.addtime','<',$time_end];
        }

        if(!empty($enable)){
            if($enable == -1){
                $enable = 0;
            }
            $where[] = ['u.enable','=',$enable];
        }
        
        if(!empty($username)){
            $where[] = ['u.username','=',$username];
        }

        if(!empty($phone)){
            $where[] = ['u.phone','=',$phone];
        }
        
        if(!empty($type)){
            $where[] = ['u.type','=',$type];
        }
        
        /*if(!empty($store_name)){
            $where[] = ['s.store_name','like','"'.$store_name.'"'];
        }*/
        

            
        $user = new User();
        $listinfo = $user->listUser($page,$num,$where)->toArray();
        
        $count = $user->countUser($where); 
     

        foreach ($listinfo as $key => $value) {

            
            
            if($value['enable']==0){
                $listinfo[$key]['user_enable'] = '未启用';
            }
            elseif ($value['enable']==1) {
                $listinfo[$key]['user_enable'] = '启用';
            }

            if(!empty($value['addtime'])){
                $listinfo[$key]['addtime'] = date( "Y-m-d H:i:s",$value['addtime']); 
            }
//            if(!empty($value['lasttime'])){
//                $listinfo[$key]['lasttime'] = date( "Y-m-d H:i:s",$value['lasttime']); 
//            }
            
        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $listinfo, 'count' => $count]);

    }
    
    //修改管理员用户名,密码
    public function userPwe() {
        $a_uid = Request::param('a_uid');
        $uid = Request::param('uid');
        $pw = Request::param('pw');
//        $old_pw = Request::param('old_pw');
        $username = Request::param('user');
        $user = new User();
        if(!empty($uid)){
            $where[] = ["u.uid","=",$uid];
        }
        
        if(!empty($username)){
            $where[] = ["u.username","=",$username];
        }
        
        $find = $user->find($where);
        if(!empty($find)){
            $find = $find->toArray();
            $encryption = $find['encryption'];
            $password = $find['password'];
        }
        else{
            return json(['code' => 0, 'msg' => lang('用户不存在'), 'data' => '', 'count' => '']);
        }

        
        $pw = $this->encPassword($pw,$encryption);//密码
        if(!empty($username)){
            $update = ['username' => $username];
            $user->userUpdate($uid,$update);
        }
        if(!empty($pw)){
            $update = ['password' => $pw];
            $user->userUpdate($uid,$update);
        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    //添加管理员
    public function add_user(){
        $a_uid = Request::param('a_uid');
        if($a_uid != 1 ){
            //return json(['code' => 0, 'msg' => lang('该账号无操作权限'), 'data' =>'', 'count' => '']);
        }
        $username = Request::param('user');
        $pw = Request::param('pw');
        $time = time();
        $user = new User();
        
        $where[] = ["username","=",$username];
        $find = $user->find($where);
        if(!empty($find)){
            return json(['code' => 0, 'msg' => lang('用户已存在'), 'data' => '', 'count' => '']);
        }

        $encryption = self::createNonceStr($length = 4);
        $password = self::encPassword($pw,$encryption);//密码
        $data = [
                'username' => $username,
                'password' => $password,
                'type' => 2,
                'encryption' => $encryption,
                'addtime' => $time,
            ];
        $user->add_user($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    
    //人员修改
    public function userEdit(){
        $user = new User();
        $a_uid = Request::param('a_uid');
        if($a_uid != 1 ){
            //return json(['code' => 0, 'msg' => lang('该账号无操作权限'), 'data' =>'', 'count' => '']);
        }
        $uid = Request::param('uid');
        
        
//        $username = Request::param('user');
        
        $enable = Request::param('enable');
        if($enable == -1){
            $enable = 0;
        }
        $update = [
//            'username' => $username,

            'enable' => $enable,
        ];

        $user->userEdit($uid,$update);

        return json(['code' => 1, 'msg' => lang(''), 'data' =>'', 'count' => '']);
    }

    
    //用户详情
    public function userInfo(){
        $uid = Request::param('uid');
        $user = new User();
        $where[] = ['u.uid','=',$uid];
        $return = $user->find($where)->toArray();//用户详情 会员等级
        $return['addtime'] = date( "Y-m-d H:i:s",$return['addtime']); 
        
        if(isset($return['lasttime'])){
            $return['lasttime'] = date( "Y-m-d H:i:s",$return['lasttime']); 
        }
        

        return json(['code' => 1, 'msg' => lang(''), 'data' => $return, 'count' => '']);

    }


}
